package com.ssyt.hiring.talent.service;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class TalentService {

	/**
	 * 求职者登录
	 * 
	 * @param code
	 * @param password
	 * @return
	 */
	public TalentEntity login(String code, String password) {
		return DBUtil.getUniqueResult(TalentEntity.class,
				"SELECT * FROM talent WHERE code=? AND password=?", code,
				password);
	}

	/**
	 * 找回密码时，设置新密码
	 * 
	 * @param accountType
	 * @param email
	 * @param newPassword
	 */
	public void updatePassword(String accountType, String email,
			String newPassword) {
		if (Constants.HIRING_TALENT.equals(accountType)) {
			DBUtil.executeUpdate("UPDATE talent SET password=? WHERE email=?",
					newPassword, email);
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			// FIXME ...
		} else {
			throw new RuntimeException("不存在的用户类型：" + accountType);
		}
	}

	/**
	 * 检查邮箱地址是否存在
	 * 
	 * @param accountType
	 * @param email
	 * @return
	 */
	public boolean isExistEmail(String accountType, String email) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					"SELECT * FROM talent WHERE email=?", email);
			if (talent != null) {
				result = true;
			}
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			// FIXME ...
		} else {
			throw new RuntimeException("不存在的用户类型：" + accountType);
		}
		return result;
	}

	/**
	 * 更新帐号对应的私钥和过期时间
	 * 
	 * @param email
	 * @param accountType
	 * @param outDate
	 * @param securityKey
	 */
	public void updateSecurityKeyAndEmailLinkOutDate(String email,
			String accountType, long outDate, String securityKey) {
		if (Constants.HIRING_TALENT.equals(accountType)) {
			DBUtil.executeUpdate(
					"UPDATE talent SET email_link_out_date=?, security_key=? WHERE email=?",
					outDate, securityKey, email);
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			// FIXME ...
		} else {
			throw new RuntimeException("不存在的用户类型：" + accountType);
		}
	}

	/**
	 * 判断用户请求的修改密码地址是否已过期
	 * 
	 * @param email
	 * @param accountType
	 * @return false 没过期， true 已过期
	 */
	public boolean isEmailLinkExpired(String email, String accountType) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					"SELECT email_link_out_date FROM talent WHERE email=?",
					email);
			if (talent == null
					|| talent.getEmailLinkOutDate() < System
							.currentTimeMillis()) {
				result = true;
			}
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			// FIXME ...
		} else {
			throw new RuntimeException("不存在的用户类型：" + accountType);
		}
		return result;
	}

	/**
	 * 判断邮箱和密钥是否匹配
	 * 
	 * @param email
	 * @param accountType
	 * @param securityKey
	 * @return
	 */
	public boolean isMatched(String email, String accountType,
			String securityKey) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					"SELECT * FROM talent WHERE email=? AND security_key=?",
					email, securityKey);
			if (talent != null) {
				result = true;
			}
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			// FIXME ...
		} else {
			throw new RuntimeException("不存在的用户类型：" + accountType);
		}
		return result;
	}
}
